package team1.project2.vmlogs;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LineProcessor {
	
	public static final String	SYS_LOG_REGEX	   = "[0-9]{4}-([0][0-9]|[1][0-2])-([0][0-9]|[1][0-9]|[2][0-9]|[3][0-1])\\w{3}:[0-9]{2}:\\w{3}\\s(.*)";
	public static final Pattern	SYS_LOG_PATTERN	   = Pattern
	                                                       .compile(
	                                                               SYS_LOG_REGEX,
	                                                               Pattern.CASE_INSENSITIVE
	                                                                       | Pattern.DOTALL);
	
	public static final String	KERNEL_LOG_REGEX	= "[0-9]{4}-([0][0-9]|[1][0-2])-([0][0-9]|[1][0-9]|[2][0-9]|[3][0-1])\\w{3}:[0-9]{2}:[0-9]{2}.\\w{4}"
	                                                       + "\\s[a-zA-Z0-9]+:[0-9]+\\)[a-zA-Z0-9]+:\\s[a-zA-Z0-9,\\s]+:\\s(.*)";
	public static final Pattern	KERNEL_LOG_PATTERN	= Pattern
	                                                       .compile(
	                                                               KERNEL_LOG_REGEX,
	                                                               Pattern.CASE_INSENSITIVE
	                                                                       | Pattern.DOTALL);
	
	public SysMessage parseSysLogLines(String line) throws ParseException {
		// line =
		// "2012-10-27T04:40:25.603Z cpu1:1163517)Net: 1652: connected Pranay eth0 to VM Network, portID 0x10001dd";
//		line = "2012-10-30T04:16:12Z Unknown: name=Pray_Backup_Pool:_remoteMoRef=vim.ResourcePool:pool20', 'resgroup-720': '_id=resgroup-720:_name=Pooja_Pool:_remoteMoRef=vim.ResourcePool:pool21', 'resgroup-737': '_id=resgroup-737:_name=Stuti_Backup_Pool:_remoteMoRef=vim.ResourcePool:pool22', 'resgroup-8': '_id=resgroup-8:_name=Resources:_remoteMoRef=vim.ResourcePool:ha-root-pool', 'resgroup-929': '_id=resgroup-929:_name=Pooja_Clone_pool:_remoteMoRef=vim.ResourcePool:pool23', 'resgroup-931': '_id=resgroup-931:_name=Krishna_Clone_Pool:_remoteMoRef=vim.ResourcePool:pool24', 'resgroup-934': '_id=resgroup-934:_name=Karthik_Pool:_remoteMoRef=vim.ResourcePool:pool25'}";
		Matcher sysLogEntryMatcher = SYS_LOG_PATTERN.matcher(line);
		
		SysMessage sysMessage = new SysMessage();
		if (sysLogEntryMatcher.matches()) {
			String date = line.substring(0, line.indexOf(" "));
			// System.out.println("data ="+date);
			// TODO handle UTC date
			Date dateTime = VmlogsUtils.getUTCTime2(date);
			System.out.println("Date " + dateTime);
			
			String event = line.substring(line.indexOf(" ") + 1);
			sysMessage.setDateTime(dateTime);
			sysMessage.setEvent(event);
			
		} else {
			System.out.println(line + " : couldn't be parsed");
//			throw new RuntimeException();
			
		}
		
		return sysMessage;
		
	}
	
	public KernelMessage parseKernelLogLines(String line) throws ParseException {
		// line =
		// "2012-10-27T04:40:25.603Z cpu1:1163517)Net: 1652: connected Pranay eth0 to VM Network, portID 0x10001dd";
		// line =
		// "2012-11-10T01:28:12.750Z cpu3:1971416)World: vm 1971417: 1260: Starting world vmm0:VCenterTeam_Master_(ALT) with flags 4008";
		Matcher kernelLogEntryMatcher = KERNEL_LOG_PATTERN.matcher(line);
		
		KernelMessage kernelMessage = new KernelMessage();
		if (kernelLogEntryMatcher.matches()) {
			String date = line.substring(0, line.indexOf(" "));
			// System.out.println("data ="+date);
			// TODO handle UTC date
			Date dateTime = VmlogsUtils.getUTCTime(date);
			System.out.println("Date " + dateTime);
			
			String remainingStr = line.substring(line.indexOf(" ") + 1);
			String cpuDetails = remainingStr.substring(0,
			        remainingStr.indexOf(")"));
			String cpuOrCoreName = cpuDetails.substring(0,
			        cpuDetails.indexOf(":"));
			String wid = cpuDetails.substring(cpuDetails.indexOf(":") + 1);
			System.out.println("cpuName =" + cpuOrCoreName + " cpuId=" + wid);
			
			remainingStr = remainingStr
			        .substring(remainingStr.indexOf(")") + 1);
			// System.out.println("remainingStr ="+remainingStr);
			String elements[] = remainingStr.split(":");
			String codeReportingMessage = elements[0].trim();
			System.out.println("codeReportingMessage =" + codeReportingMessage);
			String lineOfReportingMessage = elements[1].trim();
			System.out.println("lineOfReportingMessage="
			        + lineOfReportingMessage);
			String messageContent = "";
			if (elements.length == 3) {
				messageContent = elements[2].trim();				
			} else if (elements.length > 3) {
				for (int i = 2; i < elements.length; i++) {
					messageContent = messageContent + elements[i].trim() + ":";
                }
				messageContent = messageContent.substring(0,messageContent.length()-1);
				
			}
			
			System.out.println("messageContent =" + messageContent);
			// System.out.println("cpuDetails ="+cpuDetails);
			kernelMessage.setDateTime(dateTime);
			kernelMessage.setUptimeOfHost(date);
			kernelMessage.setCpuOrCoreName(cpuOrCoreName);
			kernelMessage.setWid(wid);
			kernelMessage.setCodeReportingMessage(codeReportingMessage);
			kernelMessage.setLineOfReportingMessage(lineOfReportingMessage);
			kernelMessage.setMessageContent(messageContent);
			
		} else {
			System.out.println(line + " : couldn't be parsed");
			throw new RuntimeException();
			
		}
		
		return kernelMessage;
		
	}
	
}
